﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Samples.Location;

namespace GpSpotMe
{
    public partial class GpsDataPanel : UserControl
    {
        private GpsPosition p;

        public GpsDataPanel()
        {
            InitializeComponent();
        }

        public GpsPosition Datasource
        {
            get { return p; }
            set { p = value; }
        }

        public void DataBind()
        {
            lblSat.Text = string.Format("{0} ; {1}",
                p != null && p.SatellitesInViewCountValid ? p.SatellitesInViewCount.ToString() : string.Empty,
                p != null && p.SatelliteInSolutionCountValid ? p.SatelliteInSolutionCount.ToString() : string.Empty);
            lblVdlop.Text = string.Format("{0} / {1} / {2}",
                p != null && p.HorizontalDilutionOfPrecisionValid ? p.HorizontalDilutionOfPrecision.ToString() : string.Empty,
                p != null && p.VerticalDilutionOfPrecisionValid ? p.VerticalDilutionOfPrecision.ToString() : string.Empty,
                p != null && p.PositionDilutionOfPrecisionValid ? p.PositionDilutionOfPrecision.ToString() : string.Empty);
            lblFixCual.Text = String.Format("{0} ; {1}",
                p != null ? p.FisType.ToString() : string.Empty,
                p != null ? p.FixQuality.ToString() : string.Empty);
            lblLat.Text = string.Format("{0}º ; {1}º",
                p != null && p.LatitudeValid ? p.Latitude.ToString("F5") : string.Empty,
                p != null && p.LongitudeValid ? p.Longitude.ToString("F5") : string.Empty);
            lblHeight.Text = string.Format("{0}m ; {1}m",
                p != null && p.EllipsoidAltitudeValid ? p.EllipsoidAltitude.ToString("F2") : string.Empty,
                p != null && p.SeaLevelAltitudeValid ? p.SeaLevelAltitude.ToString("F2") : string.Empty);
            lblTime.Text = string.Format("{0} ",
                p != null && p.TimeValid ? p.Time.ToString() : string.Empty);
            lblSpeed.Text = string.Format("{0} m/s",
                p != null && p.SpeedValid ? p.Speed.ToString("F2") : string.Empty);
            lblHeading.Text = string.Format("{0}º {1}",
                p != null && p.HeadingValid ? p.Heading.ToString("F2") : string.Empty,
                p != null && p.HeadingValid ? direction(p.Heading) : string.Empty);

            if (p != null && p.LatitudeValid && p.LongitudeValid)
            {
                double easting, northing;
                int zone;
                GeoTrans.Converter.PolarWGS84ToUTMinED50(p.Latitude, p.Longitude, 0, out easting, out northing, out zone);
                lblUtmX.Text = string.Format("{0}m {1}m {2}", easting.ToString("F0"), northing.ToString("F0"), zone);

            }
            else
            {
                lblUtmX.Text = string.Empty;
            }
        }

        private string direction(double heading)
        {
            double hc = heading + 22.5;
            int d = (int)Math.Floor(hc / 45.0);
            switch (d)
            {
                case 0: return "N";
                case 1: return "NE";
                case 2: return "E";
                case 3: return "SE";
                case 4: return "S";
                case 5: return "SO";
                case 6: return "O";
                case 7: return "NO";
                case 8: return "N";
                default: return string.Empty;
            }
        }
    }
}
